﻿function XmlObject() {
    var oXml = new ActiveXObject("Microsoft.XMLDOM");
    this.getXmlObj = function() {
        return oXml;
    }
    this.InitXmlObj = function() {
        oXml = new ActiveXObject("Microsoft.XMLDOM"); //对象
    }
    this.isTop = function(oNode) {
        if (oNode === oXml) return true;
        return false;
    }
    this.AddTextNode = function(oParent, sKey, sValue) {
        if (this.isTop(oParent) == true) {
            //为顶级对象添加的节点不能超过一个
            if (oXml.childNodes.length > 0) {
                //已经存在根节点，不继续添加
                return null;
            }
        }
        var oNewNode = oXml.createElement(sKey);
        var oTextNode = oXml.createTextNode(sValue);
        oParent.appendChild(oNewNode);
        oNewNode.appendChild(oTextNode);
        return oNewNode;
    }
    this.AddAttrNode = function(oParentNode, sNodeName, sAttr, sValue) {
        if (this.isTop(oParentNode) == true) {
            //为顶级对象添加的节点不能超过一个
            if (oXml.childNodes.length > 0) {
                //已经存在根节点，不继续添加
                return null;
            }
        }
        var oNewNode = oXml.createElement(sNodeName);
        oParentNode.appendChild(oNewNode);
        oNewNode.setAttribute(sAttr, sValue);
        return oNewNode;
    }
    this.AddAttrTextNode = function(oParentNode, sNodeName, sNodeText, sAttrName, sAttrValue) {
        if (this.isTop(oParentNode) == true) {
            //为顶级对象添加的节点不能超过一个
            if (oXml.childNodes.length > 0) {
                //已经存在根节点，不继续添加
                return null;
            }
        }
        var oNewNode = oXml.createElement(sNodeName);
        var oTextNode = oXml.createTextNode(sNodeText);
        oParentNode.appendChild(oNewNode);
        oNewNode.setAttribute(sAttrName, sAttrValue);
        oNewNode.appendChild(oTextNode);
        return oNewNode;
    }
    this.getXmlStr = function() {
        return oXml.xml;
    }
}
//***************获取Table中的数据，并提供Xml内容的支持类***********
//获取指定id对应的Table对象
function getTable(sTableId) {
    var oTable = document.getElementById(sTableId);
    if (!oTable) {
        //如果获取不是对象，而是不存在（null）
        return null;
    }
    return oTable;
}
//返回属性满足要求的所有行的数组对象
function getRows(oTable, sJuageAttr, sFitValue) {
    var nIndex = 0;
    var oRows = new Array();
    for (nIndex = 0; nIndex < oTable.rows.length; nIndex++) {
        if (oTable.rows[nIndex].getAttribute(sJuageAttr) == sFitValue) {
            //add to array
            oRows.push(oTable.rows[nIndex]);
        }
        else if (!sJuageAttr || !sFitValue) {
            //如果第二、三任何一个数据不存在，则表示不过滤
            oRows.push(oTable.rows[nIndex]);
        }
    }
    return oRows;
}
//返回一行的所有的列的属性值
//依赖于：getUseAttributes
function getRowProperty(oRow) {
    var oPropertys = [];
    var nIndex = 0;
    for (nIndex = 0; nIndex < oRow.cells.length; nIndex++) {
        oPropertys.push(getUseAttributes(oRow.cells[nIndex]));
    }
    return oPropertys;
}
//返回的是键值对的集合,name:value;
//函数返回所有的有意义的属性和属性值，默认的空白属性和属性值都不会返回
function getUseAttributes(oUnit) {
    var nIndex = 0;
    var oAttrs = [];
    var oTmpData = {};
    for (nIndex = 0; nIndex < oUnit.attributes.length; nIndex++) {
        var oAttribute = oUnit.attributes[nIndex];
        if (oAttribute.value != "null" && !oAttribute.value == false) {
            oTmpData = { name: oAttribute.name, value: oAttribute.value };
            oAttrs.push(oTmpData);
        }
    }
    return oAttrs;
}
function getRowData(oRow) {
    var oRowData = [];
    var nIndex = 0;
    for (nIndex = 0; nIndex < oRow.cells.length; nIndex++) {
        if (!oRow.cells[nIndex].getAttribute("name")) {
            var oDic = { name: "name" + nIndex, value: oRow.cells[nIndex].innerHTML };
            oRowData.push(oDic);
        }
        else {
            var oDic = { name: oRow.cells[nIndex].getAttribute("name"), value: oRow.cells[nIndex].innerHTML };
            oRowData.push(oDic);
        }
    }
    oRowData.appendData = function(oXml, oTopNode) {
        var oNewNode = oXml.createElement("data");
        var nIndex = 0;
        oTopNode.appendChild(oNewNode);
        for (nIndex = 0; nIndex < oRowData.length; nIndex++) {
            var oNode = oXml.createElement("data-collum");
            var oNodeText = oXml.createTextNode(oRowData[nIndex].value);
            oNode.setAttribute("name", oRowData[nIndex].name);
            oNode.appendChild(oNodeText);
            oNewNode.appendChild(oNode);
        }
        return;
    };
    return oRowData;
}

function getTableData(sTableId, sRowType, sTypeValue) {
    var nIndex = 0;
    var oTableInfo = [];
    var oRows =getRows(getTable(sTableId), sRowType, sTypeValue);
    for (nIndex = 0; nIndex < oRows.length; nIndex++) {
        oTableInfo.push(getRowData(oRows[nIndex]));
    }
    oTableInfo.createXmlObj = function() {
        var sXml = "";
        var nIndex = 0;
        var oXml = new ActiveXObject("Microsoft.XMLDOM");   //先创建好一个空的Xml对象
        var oTopNode = oXml.createElement("SunSoft_Data");  //创建一个顶级节点
        oXml.appendChild(oTopNode);                         //将这个顶级节点加到Xml对象上
        //oTopNode是顶级Node（节点）
        for (nIndex = 0; nIndex < oTableInfo.length; nIndex++) {
            oTableInfo[nIndex].appendData(oXml, oTopNode);  //为顶级节点增加子节点，每个子节点代表一行的数据，调用的是oRowData.appendData方法
        }
        return oXml;
    }
    return oTableInfo;
}